package com.shiguiwu.springboot3.search.util;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: stone
 * @date: Created by 2023/12/17 17:02
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.search.util
 */
public class MyClassUtil {


    public static class DynamicCompiler {

        public static Pattern CLASS_PATTERN = Pattern.compile("public\\s+class\\s+(\\w+)");

        public static JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();


        public static Map<String, byte[]> compile(String javaSrc) throws Exception {
            Matcher matcher = CLASS_PATTERN.matcher(javaSrc);
            if (!matcher.find()) {
                throw new IllegalArgumentException("class 源代码错误！！");
            }
            return compile(matcher.group(1) + ".java", javaSrc);
        }

        private static Map<String, byte[]> compile(String javaName, String javaSrc) throws Exception {
            StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
            Class<?> aClass = Class.forName("jdk.jshell.MemoryFileManager");
            Constructor<?> constructor = aClass.getConstructor(StandardJavaFileManager.class);
            Object o = constructor.newInstance(fileManager);
            //MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager);
            return null;

        }
    }
}
